package com.lc.config;

import com.lc.pojo.PmsProductCategory;
import com.lc.pojo.PmsSku;
import com.lc.service.PPCService;
import com.lc.service.PmsSkuService;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 初始化只读数据，存在缓存中
 * @Author lc
 */

@Component
public class InitData {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private PPCService ppcService;

    @Resource
    private PmsSkuService pmsSkuService;

    @PostConstruct
    public void initData(){
        System.out.println("============加载数据============");
        //存分类信息
        List<PmsProductCategory> PPCList = ppcService.getClassifyPPC();
        //redis中以字符串的形式存在
        redisTemplate.opsForValue().set("PPCList",PPCList);

        //存储SKU信息 Hash
        List<PmsSku> skuList = pmsSkuService.list();

        //List转Map集合
        Map<String, PmsSku> collect = skuList.stream().collect(Collectors.toMap(pmsSku -> pmsSku.getId().toString(), PmsSku -> PmsSku));
        redisTemplate.opsForHash().putAll("PmsSKU",collect);

        //存储其他初始化信息

    }

}
